package com.midea.ai.air.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.midea.ai.air.R;
import com.midea.ai.air.view.wheelview.OnWheelScrollListener;
import com.midea.ai.air.view.wheelview.WheelAdapter;
import com.midea.ai.air.view.wheelview.WheelView;

/**
 * Created by ouyangdi on 2016/4/23.
 * 底部弹出对话框（时间设定）Dialog
 */
public class NumberPickerDialog {
    private Context mContext;
    private Dialog mDialog;
    private WheelView mWheelView;

    public NumberPickerDialog(Context context) {
        this.mContext = context;
    }

    /**
     *
     * @return
     */
    public NumberPickerDialog builder() {
        //获取Dialog布局
        View view = View.inflate(mContext, R.layout.layout_set_time, null);

        //获取布局中的控件
        mWheelView = (WheelView)view.findViewById(R.id.wheel_set_time);

        //定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.bottom_view_style);
        mDialog.setContentView(view);

        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);

        return this;
    }

    public NumberPickerDialog setCanceledOnTouchOutside(boolean cancel) {
        mDialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public NumberPickerDialog setCyclic(boolean isCyclic) {
        mWheelView.setCyclic(isCyclic);
        return this;
    }

    public NumberPickerDialog setAdapter(WheelAdapter adapter) {
        mWheelView.setAdapter(adapter);
        return this;
    }

    public NumberPickerDialog addScrollingListener(OnWheelScrollListener listener) {
        mWheelView.addScrollingListener(listener);
        return this;
    }

    public NumberPickerDialog setOnDismissListener(DialogInterface.OnDismissListener listener) {
        mDialog.setOnDismissListener(listener);
        return this;
    }

    public void setCurrentItem(int index) {
        mWheelView.setCurrentItem(index);
    }

    public void show() {
        mDialog.show();

    }

}
